package com.tbit.uqbike.webmanager.util;

import com.tbit.common.constant.CommonConstant;
import com.tbit.uqbike.constant.Constants;
import com.tbit.uqbike.constant.MachineType;
import com.tbit.uqbike.enums.ValidEnum;
import com.tbit.uqbike.object.pojo.SuggestionExport;
import com.tbit.uqbike.object.pojo.statistics.DispatchLog;
import com.tbit.uqbike.object.pojo.statistics.MoveLog;
import com.tbit.uqbike.object.pojo.statistics.PowerLogDetail;
import com.tbit.uqbike.object.pojo.statistics.RepairLog;
import com.tbit.utils.DateTimeUtil;
import org.springframework.web.servlet.support.RequestContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 续费导出
 *
 * @author Leon
 * 2015-6-10 下午02:34:54
 */
public class PowerLogExportUtil {
    public static final Map<Integer, String> map = new HashMap<>();

    static {
        map.put(0, "转把手");
        map.put(1, "车灯");
        map.put(2, "线路");
        map.put(3, "车篮");
        map.put(4, "车轮");
        map.put(5, "车撑");
        map.put(6, "刹车");
        map.put(7, "车座");
        map.put(8, "二维码");
        map.put(9, "电池");
        map.put(10, "脚蹬");
        map.put(11, "前叉");
        map.put(12, "挡泥板");
        map.put(13, "控制器");
        map.put(14, "中控");
        map.put(15, "响铃铛");
        map.put(16, "电池锁");
        map.put(50, "其它");
    }

    /**
     * 续费数据导出
     */
    public static List<String[]> getExportContent(List<PowerLogDetail> powers, List<MoveLog> moves,
                                                  List<DispatchLog> dispatchLogs, List<RepairLog> repairLogs, RequestContext context) {
        List<String[]> result = new ArrayList<>();
        /*标题*/
        String[] title;
        if (null != powers) {
            /*标题*/
            title = new String[]{context.getMessage("excel.col.userCode"), context.getMessage("excel_head_ridelog_machineType"),
                    "绑定电池编号", "解绑电池编号", context.getMessage("excel_head_ridecard_name"),
                    context.getMessage("excel_head_ridelog_phone"), context.getMessage("excel_head_powerChange_time"),
                    context.getMessage("excel_head_powerChange_before"), context.getMessage("excel_head_powerChange_after"),
                    context.getMessage("excel_head_powerIsValid"), "换电区间", context.getMessage("excel_head_ridelog_remark")};
            result.add(title);
            for (PowerLogDetail e : powers) {
                String machineType = "";
                if (e.getMachineType().equals(MachineType.ELECTRICAR)) {
                    machineType = "电动车";
                } else if (e.getMachineType().equals(MachineType.BICYCLE)) {
                    machineType = "自行车";
                }
                String auditType = e.getAuditorId() == null || e.getAuditorId() <= 1 ? "系统" : "人工";
                String[] content = {e.getUserCode(), machineType, e.getBatteryNONew(), e.getBatteryNOOld(), e.getName(), e.getPhone(), DateTimeUtil.DateTimeToString(e.getChangeTime()), e.getOldSoc() + "%", e.getNewSoc() + "%", e.getIsValid() == 0 ? auditType + "-无效" : auditType + "-有效", e.getPowerRange(), e.getRemark()};
                result.add(content);
            }
        } else if (null != moves) {
            /*标题*/
            title = new String[]{context.getMessage("excel.col.userCode"), context.getMessage("excel_head_ridecard_name"), context.getMessage("excel_head_ridelog_phone"), context.getMessage("excel_head_ridelog_startTime"), context.getMessage("excel_head_ridelog_finishTime"), context.getMessage("excel_head_site_name"), context.getMessage("excel_head_machine_status"), context.getMessage("excel_head_ridelog_remark")};
            result.add(title);
            moves.forEach(e -> {
                String[] content = {e.getUserCode(), e.getName(), e.getPhone(),
                        DateTimeUtil.DateTimeToString(e.getStartTime()), DateTimeUtil.DateTimeToString(e.getEndTime()),
                        /*TXHttpClient.getAddress(e.getStartLon(), e.getStartLat()), TXHttpClient.getAddress(e.getEndLon(), e.getEndLat()),*/
                        e.getParkPointName(), e.getIsValid() == 1 ? context.getMessage("excel_head_deposit_valid") : context.getMessage("excel_head_deposit_invalid"), e.getRemark()
                };
                result.add(content);
            });
        } else if (null != dispatchLogs) {
            /*标题*/
            title = new String[]{context.getMessage("excel.col.userCode"), context.getMessage("excel_head_ridecard_name"), context.getMessage("excel_head_ridelog_phone"), context.getMessage("excel_head_dispatch_time"), context.getMessage("excel_head_ridelog_finishTime"), context.getMessage("excel_head_old_site"), context.getMessage("excel_head_new_site"), "调度归属星期", "调度前24小时内有效订单数", "调度后24小时内有效订单数", "调度后首次产出订单时间", "调度直线距离(米)", "调度轨迹距离(米)","调度归属时间段", context.getMessage("excel_head_ridelog_machineType"), "审核状态", "闲置区间", context.getMessage("excel_head_ridelog_remark")};
            result.add(title);
            dispatchLogs.forEach(e -> {
                String machineType = "";
                if (e.getMachineType() == 0) {
                    machineType = "电单车";
                } else {
                    machineType = "自行车";
                }
                String auditType = e.getAuditorId() == null || e.getAuditorId() <= 1 ? "系统" : "人工";
                String[] content = {e.getUserCode(), e.getName(), e.getPhone(), DateTimeUtil.DateTimeToString(e.getDispatchTime()),
                        DateTimeUtil.DateTimeToString(e.getFinishTime()), e.getOldParkPoint(), e.getNewParkPoint(), e.getDispatchVestingWeek(), e.getBeforeValidOrder() == null ? "未知" : e.getBeforeValidOrder().toString(), e.getAfterValidOrder() == null ? "未知" : e.getAfterValidOrder().toString(), e.getFirstOrderTime(), e.getStraightDistance() == null ? "未知" : e.getStraightDistance() == null ? "未知" : e.getStraightDistance().toString(), e.getDistance() == null ? "未知" : e.getDistance().toString(), e.getDispatchVestingTimePeriod(), machineType, auditType + "-" + ValidEnum.getStatusName(e.getIsValid()), e.getDispatchRange(), e.getRemark()};
                result.add(content);
            });
        } else {
            /*标题*/
            title = new String[]{"车辆编号", "维修人员名称", "添加时间", "接单时间", "维修类型", "维修状态", "备注"};
            result.add(title);
            repairLogs.stream().forEach(e -> {
                String[] types = e.getRepairTypes().split(",");
                StringBuilder str = new StringBuilder("");
                int i = 0;
                for (; i < types.length; i++) {
                    if (null != map.get(Integer.parseInt(types[i]))) {
                        str.append(map.get(Integer.parseInt(types[i])) + ",");
                    }
                }
                String s = str.toString();

                if (!"".equals(s) && s.length() - 1 == s.lastIndexOf(",")) {
                    s = s.substring(0, s.length() - 1);
                }
                String[] content = {e.getUserCode(), e.getUserName(), DateTimeUtil.DateTimeToString(e.getAddTime()),
                        DateTimeUtil.DateTimeToString(e.getAcceptTime()), s,
                        e.getRepairState() == 1 ? "维修完成" : "未维修", e.getRemark()
                };
                result.add(content);
            });
        }
        return result;
    }


    /**
     * 意见反馈导出
     *
     * @param list
     * @return
     */
    public static List<String[]> suggestExport(List<SuggestionExport> list) {
        List<String[]> result = new ArrayList<String[]>();

        /*标题*/
        String[] title = {"类型", "内容", "联系方式", "回复", "状态", "添加时间", "处理时间"};
        result.add(title);
        for (SuggestionExport e : list) {
            String handleTime = "";
            if (e.getHandleTime() != null) {
                handleTime = DateTimeUtil.DateTimeToString(e.getHandleTime());
            }
            String[] content = {e.getTitle(), e.getContent(), e.getPhone(), e.getReply(), e.getState(), DateTimeUtil.DateTimeToString(e.getAddTime()), handleTime};
            result.add(content);
        }
        return result;
    }



}
